package com.hibernate.manage.tpl;
 
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.hibernate.modle.tp.AbstractInfDate;

public abstract class  CatchDateManage<K, T extends AbstractInfDate<K>>{
	private  LoadingCache<K, T> dateCatch;
	public LoadingCache<K, T> getDateCatch() {
		return dateCatch;
	}
	// 缓存接口这里是LoadingCache，LoadingCache在缓存项不存在时可以自动加载缓存
	public CatchDateManage(Class<T> theClass){
	    // 缓存接口这里是LoadingCache，LoadingCache在缓存项不存在时可以自动加载缓存
         dateCatch
        // CacheBuilder的构造函数是私有的，只能通过其静态方法newBuilder()来获得CacheBuilder的实例
                = CacheBuilder.newBuilder()
                		/*设置缓存容器的初始容量大小为10*/  
                        .initialCapacity(10)      
                        .concurrencyLevel(10) 
                        // build方法中可以指定CacheLoader，在缓存不存在时通过CacheLoader的实现自动加载缓存
                        .build(new CacheLoader<K, T>() {
                            @SuppressWarnings("deprecation")
							@Override
                            public T load(K key) throws Exception {
                            	T t = readInit(key);
                            	if(t == null) {
                            		return (T)theClass.newInstance();
                            	}else {
                            		return  t;
                            	}
                            }
                        });
	}
	public abstract T readInit(K i) throws InstantiationException, IllegalAccessException;
}
